package com.uni.action;

import com.uni.entity.Commodity;
import com.uni.entity.CommodityType;
import com.uni.entity.Page;
import com.uni.service.Service;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * --------------project-------------
 *
 * @Author Unirithe  - Mr.Chao
 * - CSDN:
 * https://blog.csdn.net/Unirithe
 * - GitHub:
 * https://github.com/unirithe
 * @create 2021/12/22 13:28
 * ------------------------------------------
 */
@WebServlet(name = "CommodityQueryAction", urlPatterns = "/shop_center")
public class CommodityQueryAction extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        Page page = (Page)session.getAttribute("pageInfo");
        // 若是第一次访问，则初始化page对象
        if(page == null) {
            // 初始化
            page = new Page();
            page.setPageCount(8);
            page.setPageNow(0);
            session.setAttribute("totalCommodity", Service.commodity.queryAll().size());
        }

        int tid = -1;
        // 当前查询的商品类型，可能为空
        String tid_param = req.getParameter("ctype");
        // session域名的nowType
        Integer tid_session = (Integer) session.getAttribute("nowType");
        // 优先获取地址栏的参数
        if(tid_param == null && tid_session == null) {
            tid = -1;
        }
        else if(tid_param != null) { // 重置页面为第一页
            page.setPageNow(0);
            tid = Integer.parseInt(tid_param);
        }
        else tid = tid_session;

        // 修改分页的属性
        page.setTotalRows(Service.commodity.countByTypeId(tid));
        page.setTotalPage(page.getTotalRows() / page.getPageCount());

        // 根据商品类型分页查询商品数据
        List<Commodity> commodities = Service.commodity.query(page.getPageNow() * page.getPageCount(),
                page.getPageCount(), tid);
        // 根据ID查询商品分类的名称和发布人的名称
        for (Commodity c : commodities) {
            c.setTypeName(Service.commodity.queryTypeById(c.getTid()));
            c.setUserName(Service.user.queryUserById(c.getUid()).getName());
        }

        // 设置商品类型信息
        List<CommodityType> ctypes = Service.commodity.queryAllType();
        for (CommodityType ctype : ctypes) {
            // 根据商品类型表的ID去统计商品表中同一个类型的个数
            ctype.setCount(Service.commodity.countByTypeId(ctype.getId()));
        }
        /* 保存页面信息 */
        session.setAttribute("pageInfo", page);
        /* 保存商城的商品类型到 session 域 */
        session.setAttribute("commodityTypes", ctypes);
        session.setAttribute("nowType", tid);
        /* 保存商品的信息到 session 域 */
        session.setAttribute("commodityInfo", commodities);
        session.setAttribute("nowPage", 0);
        /* 保存当前商品的类型到session 域 */
        session.setAttribute("nowTypeName", Service.commodity.queryTypeById(tid));
        // ... 重定向
        resp.sendRedirect("shop_center.jsp");
    }
}
